"""API测试用例"""
import pytest
from fastapi.testclient import TestClient
from app.main import app
from app.database import SessionLocal, Base, engine
from app.models import ScanTask, ScanRecord, Issue, ScanIssueExtension
from datetime import datetime

client = TestClient(app)


@pytest.fixture(scope="function")
def db_session():
    """创建测试数据库会话"""
    Base.metadata.create_all(bind=engine)
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
        Base.metadata.drop_all(bind=engine)


@pytest.fixture
def test_task(db_session):
    """创建测试任务"""
    task = ScanTask(
        name="测试扫描任务",
        task_type="test_scan",
        description="测试用扫描任务",
        status="enabled"
    )
    db_session.add(task)
    db_session.commit()
    db_session.refresh(task)
    return task


def test_health_check():
    """测试健康检查"""
    response = client.get("/health")
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "healthy"


def test_root():
    """测试根路径"""
    response = client.get("/")
    assert response.status_code == 200
    data = response.json()
    assert data["name"] == "CodeQualityBoard"
    assert data["status"] == "running"


def test_get_scan_tasks():
    """测试获取扫描任务列表"""
    response = client.get("/api/v1/scan-tasks?skip=0&limit=10")
    assert response.status_code == 200
    data = response.json()
    assert data["code"] == 200
    assert "data" in data
    assert "total" in data


def test_get_scan_task_detail():
    """测试获取扫描任务详情"""
    # 先获取任务列表
    tasks_response = client.get("/api/v1/scan-tasks?skip=0&limit=1")
    if tasks_response.status_code == 200:
        tasks_data = tasks_response.json()
        if tasks_data.get("data") and len(tasks_data["data"]) > 0:
            task_id = tasks_data["data"][0]["id"]
            response = client.get(f"/api/v1/scan-tasks/{task_id}")
            assert response.status_code == 200
            data = response.json()
            assert data["code"] == 200
            assert "data" in data


def test_get_scan_task_not_found():
    """测试获取不存在的任务"""
    response = client.get("/api/v1/scan-tasks/99999")
    assert response.status_code == 404
    data = response.json()
    assert data["code"] == 404


def test_get_scan_records():
    """测试获取扫描记录列表"""
    response = client.get("/api/v1/scan-records?skip=0&limit=10")
    assert response.status_code == 200
    data = response.json()
    assert data["code"] == 200
    assert "data" in data


def test_get_field_configs():
    """测试获取字段配置"""
    # 先获取任务列表
    tasks_response = client.get("/api/v1/scan-tasks?skip=0&limit=10")
    if tasks_response.status_code == 200:
        tasks_data = tasks_response.json()
        if tasks_data.get("data") and len(tasks_data["data"]) > 0:
            task_id = tasks_data["data"][0]["id"]
            response = client.get(f"/api/v1/tasks/{task_id}/field-configs")
            assert response.status_code == 200
            data = response.json()
            assert data["code"] == 200
            assert "data" in data

